package com.mit.web.config
{
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.core.INavigatorContent;

	public class DisplayConfiguration
	{
		private static const CONFIG_FILE:String = "com/mit/web/config/DisplayConfig.xml";
		private static var me:DisplayConfiguration;

		private var pagesize:int;
		private var pagegroupSize:int;
		private var rowCount:int;
		
		private var cacheLifeCycle:int;
		
		private var dateFormat:String;
		private var timeFormat:String;
		private var datetimeFormat:String;
		
		private var maxUploadFileSize:int;
		
		private var logLevel:int;
		private var showErrorMsg:Boolean;
		private var errorTipDelaySec:int;
		
		public function DisplayConfiguration()
		{
			if(me != null)
				throw new Error("Only one instance of this class is allowed, use getInstance() insteadof the constructor.");

			var xmlURL:URLRequest = new URLRequest(CONFIG_FILE);
			var xmlLoader:URLLoader = new URLLoader(xmlURL);
			xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, loadFailed);
			xmlLoader.addEventListener(Event.COMPLETE, loadConfig);

		}
		
		public static function getInstance():DisplayConfiguration{
			if(me == null)
				me = new DisplayConfiguration();
			return me;
		}
		
		private function loadConfig(e:Event):void{
			var config:XML = XML(((e.currentTarget) as URLLoader).data);
			rowCount = parseInt(config.config.(@item == "rowcount").@value);
			pagesize = parseInt(config.config.(@item == "pagesize").@value);
			pagegroupSize = parseInt(config.config.(@item == "pagegroupsize").@value);
			cacheLifeCycle = parseInt(config.config.(@item=="cacheLifeCycle").@value);
			dateFormat = config.config.(@item=="dateFormat").@value;
			timeFormat = config.config.(@item=="timeFormat").@value;
			datetimeFormat = config.config.(@item=="datetimeFormat").@value;
			logLevel = config.config.(@item=="logLevel").@value;
			maxUploadFileSize = parseInt(config.config.(@item == "maxUploadFileSize").@value);
			showErrorMsg = Boolean(config.config.(@item == "showErrorMsg").@value);
			errorTipDelaySec = parseInt(config.config.(@item == "errorTipDelaySecond").@value);
		}

		private function loadFailed(e:IOError):void{
			
			var msg:String = e.message;
			trace(msg);
		}
		
		public function get PAGE_SIZE():int{
	
			return pagesize ;
		}
		
		public function get PAGE_GROUP_SIZE():int{
			return pagegroupSize;
		}
		
		public function get DEFAULT_ROW_COUNT():int{
			return rowCount;
		}
		
		public function get CACHE_LIFE_CYCLE():int{
			return cacheLifeCycle;
		}
		
		public function get DATE_FORMAT():String{
			return dateFormat;			
		}
		public function get TIME_FORMAT():String{
			return timeFormat;			
		}
		public function get DATETIME_FORMAT():String{
			return datetimeFormat;			
		}
		public function get LOG_LEVEL():int{
			return logLevel;
		}
		
		public function get DEFAULT_MAX_UPLOAD_KB():int{
			return maxUploadFileSize;
		}
		
		public function get SHOW_ERROR():Boolean{
			return showErrorMsg;
		}
		
		public function get ERROR_TIP_DELAY():int{
			return errorTipDelaySec * 1000;
		}
	}
}